<template>
  <div class="execution">
    <basic-container>
      <el-card class="filter-container" shadow="never">
        <div style="margin-top: 10px">
          <el-form :inline="true" size="small" label-width="140px">
            <el-form-item label="店铺">
              <el-select v-model="listQuery.storeId" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.storeId"
                  :label="item.storeName"
                  :value="item.storeId"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="用户昵称">
              <el-input v-model="listQuery.nickName"></el-input>
            </el-form-item>
            <el-form-item label="手机号码">
              <el-input v-model="listQuery.phone" placeholder="请输入手机号" oninput = "value=value.replace(/[^\d]/g,'')" maxlength="11"></el-input>
            </el-form-item>
            <el-form-item label="操作人">
              <el-input v-model="listQuery.operation"></el-input>
            </el-form-item>
            <el-form-item label="操作时间">
              <el-date-picker
                v-model="dateTimer"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button @click="getList" type="primary" size="small"
                >查询结果</el-button
              >
              <el-button @click="handleResetSearch" size="small">
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <div style="margin-top: 10px">
        <el-table
          :data="list"
          border
          :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
        >
          <el-table-column
            label="用户ID"
            align="center"
            prop="id"
          ></el-table-column>
          <el-table-column
            label="用户昵称"
            align="center"
            prop="nickName"
          ></el-table-column>
          <el-table-column
            label="用户手机号"
            align="center"
            prop="phone"
          ></el-table-column>
          <el-table-column
            label="所属店铺"
            align="center"
            prop="storeName"
          ></el-table-column>
          <el-table-column label="收益百分比" align="center" prop="earn">
            <template slot-scope="{ row }">
              <template v-if="row.edit">
                <el-input
                  placeholder="请输入收益百分比"
                  size="mini"
                  v-model="row.earn"
                  style="width: 200px"
                ></el-input>
                <el-button
                  class="cancel-btn"
                  size="mini"
                  icon="el-icon-refresh"
                  type="warning"
                  @click="cancelEdit(row)"
                >
                  取消
                </el-button>
              </template>
              <span v-else>{{ row.earn }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作人"
            align="center"
            prop="operationName"
          ></el-table-column>
          <el-table-column
            label="操作时间"
            align="center"
            prop="operationTime"
          ></el-table-column>
          <el-table-column label="操作" min-width="150" align="center">
            <template slot-scope="{ row }">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="text"
                @click="row.edit = !row.edit"
                >修改</el-button
              >
              <el-button
                size="mini"
                @click="handleUpdate(row)"
                type="text"
                v-if="row.edit"
                plain
                >保存</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.size"
          :page-sizes="[5, 10, 15]"
          :current-page.sync="listQuery.current"
          :total="total"
        >
        </el-pagination>
      </div>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import { fetchList, updateFirstMembersEarn } from "@/api/profit/oneLevel/index";
import { getStore } from "@/api/rms/rmsstore";

const defaultListQuery = {
  current: 1,
  size: 10,
  nickName: null,
  phone: null,
  storeId: null,
  operation: null,
  startTime: null,
  endTime: null,

};

export default {
  data() {
    return {
      options: [],
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      dateTimer: null,
    };
  },
  created() {
    this.getList();
    this.getStoreList();
  },
  methods: {
    getStoreList() {
      getStore().then((res) => {
        this.options = res.data.data;
      });
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getList();
    },
    getList() {
      fetchList(this.listQuery).then((res) => {
        console.log('res.data.records;',res.data.data.records)
        let list = res.data.data.records;
        this.list = list.map((v) => {
          this.$set(v, "edit", false);
          v.Title = v.earn; // 将在用户单击“取消”按钮时使用
          return v;
        });
        this.total = res.data.total;
      });
    },
    handelSeach() {
      this.listQuery.startTime = this.dateTimer[0];
      this.listQuery.endTime = this.dateTimer[1];
      this.getList();
    },
    handleResetSearch() {
      this.dateTimer = "";
      this.listQuery = Object.assign({}, this.defaultListQuery);
    },
    handleUpdate(row) {
      row.edit = false;
      this.$confirm("是否要修改百分比?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let params = {
          id: row.id,
          earn: row.earn,
        };
        updateFirstMembersEarn(params).then((res) => {
          if (res.data.code === 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
            });
            this.getList();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      });
    },
    cancelEdit(row) {
      row.earn = row.Title;
      row.edit = false;
      this.$message({
        message: "百分比已恢复原值",
        type: "warning",
      });
    },
  },
};
</script>
